<?php
	if(isset($_POST['email'])) {
		if(isset($_POST['message']) && $_POST['message']!='') {
			if ( strtoupper($_POST['validate']) == strtoupper( trim ( fread ( fopen ( FILE_SERVER.INCLUDE_DIR."captcha/".$_POST['id'].".txt", "r"), filesize ( FILE_SERVER.INCLUDE_DIR."captcha/".$_POST['id'].".txt" ) ) ) ) ) {
				$from = $_POST['name']." <".$_POST['email'].">";
				if($_POST['cc']=='1') $cc = "\r\nCc: ".$from;
				else $cc = "";
				mail(MASTER_EMAIL, CONTEST_NAME.": ".$_POST['subject'], $_POST['message'], "From: ".$from.$cc); //send out contact email
				unlink ( FILE_SERVER.INCLUDE_DIR."captcha/".$_POST['id'].".txt" );
				$msg = "<span class='success'>Thank you for for interest in the SA Blog Awards. We have received your query and will get back to you shortly.</span>";
				$post_name = $post_from_email = $post_subject = $post_msg = "";
			} else {
				$msg = "<span class='error'>Sorry, Invalid Authentication Code, Please retype it in and resubmit.</span>";
				$post_name = $_POST['name'];
				$post_from_email = $_POST['email'];
				$post_subject = $_POST['subject'];
				$post_msg = $_POST['message'];
				$id = $_POST['id'];
			}
		} else {
			$msg = "<span class='error'>Sorry, You have no message typed in. Please fill in the field before sending the contact.</span>";
			$post_name = $_POST['name'];
			$post_from_email = $_POST['email'];
			$post_subject = $_POST['subject'];
			$post_msg = $_POST['message'];
			$id = $_POST['id'];
		}
		//if executing via ajax, return results
		if(isset($_GET['ajax'])) {
			echo $msg;
			exit;
		}
	} else {
		$msg = $string = $post_name = $post_from_email = $post_subject = $post_msg = "";
		//Create an unique validation string
		$chars = "123456789ABCDEFGHIJKLMNPQRSTUVWXYZ";
		for ( $i = 0; $i < 5; $i++ ) $string .= $chars{rand(0, (strlen($chars)-1))};
		$id = time();
		fwrite ( fopen ( FILE_SERVER.INCLUDE_DIR."captcha/".$id.".txt", "w" ), $string );
	}
?>